package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.entity.ApplicationRobot;
import com.evil.common.application.dto.robot.RobotSourceDTO;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.StreamUtil;
import com.evil.common.family.enums.RCodeEnum;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 应用机器人表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationRobotMapper extends BaseMapper<ApplicationRobot> {

    Optional<ApplicationRobot> isExistById(Long id);

    default ApplicationRobot findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_ROBOT_NOT_EXIST));
    }

    List<ApplicationRobot> findByIds(@Param("ids") Collection<Long> ids);

    default Map<Long, ApplicationRobot> findMapByIds(@Param("ids") Collection<Long> ids) {
        return StreamUtil.toMapK(this.findByIds(ids), ApplicationRobot::getRobotId);
    }

    List<ApplicationRobot> findBySource(RobotSourceDTO source);

    List<ApplicationRobot> findBySources(@Param("sources") Collection<RobotSourceDTO> sources);

    boolean isExistByTriggerType(@Param("source") RobotSourceDTO source, @Param("firstTriggerType") int firstTriggerType);

    List<ApplicationRobot> findByEnterpriseId(Long enterpriseId);
}
